# -*- coding: utf-8 -*-

#    Copyright (C) 2014 by Promotux
#                       di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>

#    This file is part of OdCollect.

#    OdCollect is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    OdCollect is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with OdCollect.  If not, see <http://www.gnu.org/licenses/>.

from sqlalchemy import Table
from sqlalchemy.orm import mapper, relation
from Environment import *
from web_lib.webutils import leggiInSessione

try:
    t_operatore = Table('operatore', meta, schema=leggiInSessione(sezione="Main", chiave="schema"), autoload=True)
except:
    from data.operatore import t_operatore

from dao.Dao import Dao


class Operatore(Dao):
    """ User class provides to make a Users dao which include more used"""

    def __init__(self, req=None):
        Dao.__init__(self, entity=self)

    def filter_values(self,k,v):
        if k == 'username':
            dic = {k:t_operatore.c.username == v}
        elif k == 'password':
            dic = {k:t_operatore.c.password == v}
        elif k == 'passwordKey':
            dic = {k:t_operatore.c.password.ilike("%"+v+"%")}
        elif k == 'usern':
            dic = {k:t_operatore.c.username.ilike("%"+v+"%")}
        elif k == 'email':
            dic = {k:t_operatore.c.email.ilike("%"+v+"%")}
        elif k == 'emailEM':
            dic = {k:t_operatore.c.email == v}
        elif k == 'idEnte':
            dic = {k:t_operatore.c.id_ente == v}
        elif k == 'active':
            dic = {k:t_operatore.c.active == v}
        elif k == 'tipoOperatore':
            dic = {k:t_operatore.c.tipo_operatore == v}
        return  dic[k]

std_mapper = mapper(Operatore, t_operatore, order_by=t_operatore.c.username)
